package com.${companyName}.${projectName}.web.controller.${model};

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.${companyName}.${projectName}.vo.SimpleReturnVo;
import com.${companyName}.${projectName}.web.controller.BaseController;
import com.${companyName}.${projectName}.tools.common.JsonUtils;
import com.${companyName}.${projectName}.tools.pager.PagerModel;
import com.${companyName}.${projectName}.tools.pager.Query;

<#assign primaryKeyTypeJava="String"/> 
<#if columnFieldList??>  
	<#list columnFieldList as item>
		<#if item.column=='${primaryKeys[0]}' && item.jdbcType=='INTEGER'>
			<#assign primaryKeyTypeJava="Integer"/>
		</#if>
	</#list>
</#if>


/**
 * ${objectNameCn}Controller
 * @author ${author}
 * @date ${createTime}
 */
@Controller
@RequestMapping("/${projectName}/${model}/${abbObjectName}")
public class ${objectName}Controller extends BaseController {
	private static Logger logger = Logger.getLogger(${objectName}Controller.class);
	
	private final String nameSpace="${abbObjectName}";
	
	@Resource
	private I${objectName}Service ${fLowerObjectName}Service;
	
	//列表
	@RequestMapping("/list")
	public String list(ModelMap model,String sessionId) {
		model.addAttribute("sessionId", sessionId);
		return "/${model}/${abbObjectName}_list";
	}

	@ResponseBody
	@RequestMapping("/ajaxList")
	public String ajaxList(${objectName} ${fLowerObjectName}, Query query,String sessionId) {
		PagerModel<${objectName}> pm = null;
		try {
			pm = this.${fLowerObjectName}Service.getPagerModelByQuery(${fLowerObjectName}, query);
		} catch (Exception e) {
			logger.error("${objectName}Controller-ajaxList:",e);
			e.printStackTrace();
		}
		return JsonUtils.toJson(pm);
	}
	
	/*@RequestMapping("/addUI")
	public String addUI(String ${primaryKeysConvert[0]},ModelMap model,String sessionId) {
		return "/${model}/${abbObjectName}_update";
	}*/

	//添加
	@ResponseBody
	@RequestMapping("/add")
	public String add(${objectName} ${fLowerObjectName}, String sessionId) {
		SimpleReturnVo returnVo = new SimpleReturnVo(ERROR, "添加失败");
		try {
			User user = this.getLoginUser(sessionId);
			if (null != user && StringUtils.isNotBlank(user.getUsername())) {
				String userName=user.getUsername();
				${fLowerObjectName}.setCreator(userName);
				${fLowerObjectName}.setUpdator(userName);
				this.${fLowerObjectName}Service.insert${objectName}(${fLowerObjectName});
				returnVo = new SimpleReturnVo(SUCCESS, "添加成功");
			}else{
				returnVo = new SimpleReturnVo(ERROR, "用户信息获取失败，请重新登录");
			}
		} catch (Exception e) {
			logger.error("${objectName}Controller-add:",e);
			e.printStackTrace();
		}
		return JsonUtils.toJson(returnVo);
	}

	//添加、修改的UI
	@RequestMapping("/updateUI")
	public String updateUI(ModelMap model,String ${primaryKeysConvert[0]},String sessionId) {
		try {
			if(StringUtils.isNotBlank(${primaryKeysConvert[0]})){
				${objectName} ${fLowerObjectName}=this.${fLowerObjectName}Service.get${objectName}By${primaryKeyMerger}(id);
				model.addAttribute("${fLowerObjectName}", ${fLowerObjectName});
			}
		} catch (Exception e) {
			logger.error("${objectName}Controller-updateUI:",e);
			e.printStackTrace();
		}
		return "/${model}/${abbObjectName}_update";
	}

	//修改
	@ResponseBody
	@RequestMapping("/update")
	public String update(${objectName} ${fLowerObjectName}, String sessionId) {
		SimpleReturnVo returnVo = new SimpleReturnVo(ERROR, "修改失败");
		try {
			User user = this.getLoginUser(sessionId);
			if (null != user && StringUtils.isNotBlank(user.getUsername())) {
				String userName=user.getUsername();
				${fLowerObjectName}.setUpdator(userName);
				${fLowerObjectName}.setDelFlag(null);
				this.${fLowerObjectName}Service.update${objectName}(${fLowerObjectName});
				returnVo = new SimpleReturnVo(SUCCESS, "修改成功");
			}else{
				returnVo = new SimpleReturnVo(ERROR, "用户信息获取失败，请重新登录");
			}
		} catch (Exception e) {
			logger.error("${objectName}Controller-update:",e);
			e.printStackTrace();
		}
		return JsonUtils.toJson(returnVo);
	}
	<#assign statusFlag=false>
	<#if columnFieldList??>
	  <#list columnFieldList as item>
		<#if item.field?? && 'status'=item.field>
		<#assign statusFlag=true>
		</#if>
	  </#list>
	</#if>
	
	<#if statusFlag>
	//修改状态
	@ResponseBody
	@RequestMapping("/updateStatus")
	public String updateStatus(String ${primaryKeysConvert[0]}s,String status, String sessionId) {
		SimpleReturnVo returnVo = new SimpleReturnVo(ERROR, "修改状态失败");
		try {
			User user = this.getLoginUser(sessionId);
			if (null != user && StringUtils.isNotBlank(user.getUsername())) {
				String userName=user.getUsername();
				if (StringUtils.isNotBlank(${primaryKeysConvert[0]}s) && null != status) {
					${objectName} ${fLowerObjectName} = new  ${objectName}();
					${fLowerObjectName}.setUpdator(userName);
					${fLowerObjectName}.setDelFlag(null);
					${fLowerObjectName}.setStatus(status);
					this.${fLowerObjectName}Service.update${objectName}By${primaryKeyMerger}s(${primaryKeysConvert[0]}s, ${fLowerObjectName});
				}
				returnVo = new SimpleReturnVo(SUCCESS, "修改状态成功");
			}else{
				returnVo = new SimpleReturnVo(ERROR, "用户信息获取失败，请重新登录");
			}
		} catch (Exception e) {
			logger.error("${objectName}Controller-updateStatus:",e);
			e.printStackTrace();
		}
		return JsonUtils.toJson(returnVo);
	}
	<#else>
	/*
	//修改状态
	@ResponseBody
	@RequestMapping("/updateStatus")
	public String updateStatus(String ${primaryKeysConvert[0]}s,String status, String sessionId) {
		SimpleReturnVo returnVo = new SimpleReturnVo(ERROR, "修改状态失败");
		try {
			User user = this.getLoginUser(sessionId);
			if (null != user && StringUtils.isNotBlank(user.getUsername())) {
				String userName=user.getUsername();
				if (StringUtils.isNotBlank(${primaryKeysConvert[0]}s) && null != status) {
					${objectName} ${fLowerObjectName} = new  ${objectName}();
					${fLowerObjectName}.setUpdator(userName);
					${fLowerObjectName}.setDelFlag(null);
					${fLowerObjectName}.setStatus(status);
					this.${fLowerObjectName}Service.update${objectName}By${primaryKeyMerger}s(${primaryKeysConvert[0]}s, ${fLowerObjectName});
				}
				returnVo = new SimpleReturnVo(SUCCESS, "修改状态成功");
			}else{
				returnVo = new SimpleReturnVo(ERROR, "用户信息获取失败，请重新登录");
			}
		} catch (Exception e) {
			logger.error("${objectName}Controller-updateStatus:",e);
			e.printStackTrace();
		}
		return JsonUtils.toJson(returnVo);
	}*/
	</#if>
	
	//删除
	@ResponseBody
	@RequestMapping("/dels")
	public String dels(String ${primaryKeysConvert[0]}s,HttpServletRequest request, String sessionId) {
		SimpleReturnVo returnVo = new SimpleReturnVo(ERROR, "删除失败");
		try {
			User user = this.getLoginUser(sessionId);
			if (null != user && StringUtils.isNotBlank(user.getUsername())) {
				String userName=user.getUsername();
				if(StringUtils.isNotBlank(${primaryKeysConvert[0]}s)){
					${objectName} ${fLowerObjectName} = new  ${objectName}();
					${fLowerObjectName}.setUpdator(userName);
					this.${fLowerObjectName}Service.update${objectName}By${primaryKeyMerger}s(${primaryKeysConvert[0]}s,${fLowerObjectName});//逻辑删除
					//this.${fLowerObjectName}Service.del${objectName}By${primaryKeyMerger}s(${primaryKeysConvert[0]}s); //物理删除
				}
				returnVo = new SimpleReturnVo(SUCCESS, "删除成功");
			}else{
				returnVo = new SimpleReturnVo(ERROR, "用户信息获取失败，请重新登录");
			}
		} catch (Exception e) {
			logger.error("${objectName}Controller-dels:",e);
			e.printStackTrace();
		}
		return JsonUtils.toJson(returnVo);
	}
}
